package com.xaicode.auth.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.xaicode.auth.annotation.Log;
import com.xaicode.auth.domain.Result;
import com.xaicode.auth.domain.params.IdsParam;
import com.xaicode.auth.entity.SysDictDetail;
import com.xaicode.auth.enums.BusinessType;
import com.xaicode.auth.security.domain.params.SysDictDetailQueryParam;
import com.xaicode.auth.service.ISysDictDetailService;
import com.xaicode.auth.utils.GlobalValidator;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 字典详情接口
 * 
 * @date 2024/12/19
 */
@RestController
@RequestMapping("/api/system/dict-detail")
public class SysDictDetailController {

    @Autowired
    private ISysDictDetailService sysDictDetailService;

    @Log(title = "查看字典详情列表", businessType = BusinessType.QUERY, isSaveResponseData = false)
    @SaCheckPermission("system:dict-detail:list")
    @PostMapping("/page")
    public Result<?> page(@RequestBody SysDictDetailQueryParam param) {
        PageInfo<SysDictDetail> pageInfo = sysDictDetailService.queryPage(param);
        return Result.ok(pageInfo);
    }

    /**
     * 查询指定点内容
     *
     * @param param .
     * @return .
     */
    @PostMapping("/list")
    public Result<?> list(@RequestBody SysDictDetailQueryParam param) {
        List<SysDictDetail> list = sysDictDetailService.queryList(param);
        return Result.ok(list);
    }

    @Log(title = "新增字典详情", businessType = BusinessType.INSERT)
    @SaCheckPermission("system:dict-detail:add")
    @PostMapping("/create")
    public Result<?> create(@RequestBody SysDictDetail entity) {
        sysDictDetailService.save(entity);
        return Result.ok();
    }

    @Log(title = "修改字典详情", businessType = BusinessType.UPDATE)
    @SaCheckPermission("system:dict-detail:edit")
    @PostMapping("/update")
    public Result<?> update(@RequestBody SysDictDetail entity) {
        GlobalValidator.notNull(entity.getId(), "id不能为空");
        sysDictDetailService.updateById(entity);
        return Result.ok();
    }

    @Log(title = "删除字典详情", businessType = BusinessType.DELETE)
    @SaCheckPermission("system:dict-detail:del")
    @PostMapping("/delete")
    public Result<?> delete(@RequestBody IdsParam<Integer> ids) {
        sysDictDetailService.removeByIds(ids.getIds());
        return Result.ok();
    }

    @PostMapping("/listByDictKey")
    public Result<?> list(String dictKey) {
        List<SysDictDetail> res = sysDictDetailService.list(new LambdaQueryWrapper<SysDictDetail>()
                .eq(SysDictDetail::getDictKey, dictKey)
                .orderByAsc(SysDictDetail::getSortNum)
        );
        return Result.ok(res);
    }

}
